package com.zw.mes.module.assemble.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.asse.HalfStore;
import com.zw.mes.module.assemble.entity.AsseAssemblyMake;
import com.zw.mes.module.assemble.entity.AsseAssemblySheet;
import com.zw.mes.module.assemble.model.MakeListHotModel;
import com.zw.mes.module.assemble.model.MakeListModel;
import com.zw.mes.module.assemble.repository.AsseAssemblySheetRepository;
import com.zw.mes.module.assemble.service.AssMakeService;
import com.zw.mes.module.assemble.service.DispatchService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 装配派工单 制作件及外购件选择
 * <p>
 * 权限：
 * @Author: China.wsg
 * @Date: 2021/05/06 13:12
 */
@Controller
@RequestMapping("/assemble/assMake")
@SuppressWarnings("all")
public class AssMakeController extends BaseController {

    @Autowired
    private AssMakeService makeService;
    @Autowired
    private AsseAssemblySheetRepository assemblySheetRepository;
    @Autowired
    private DispatchService dispatchService;

    /**
     * @Author wsg
     * @Date 13:24 2021/5/6
     * @Description 装配派工单查询 页面
     **/
    @GetMapping("/dispatchSel")
    @RequiresPermissions({"xxx"})
    public String addPage(Model model) {
        return "assemble/make/dispatchSel";
    }

    /**
     * @Author wsg
     * @Date 13:29 2021/5/6
     * @Description 装配派工单查询列表（排除类型为‘自动化’数据）
     **/
    @ResponseBody
    @PostMapping("/pageList")
    @RequiresPermissions({"xxx"})
    public JsonResult pageList(AsseAssemblySheet assemblySheet) {
        startPage();
        List<AsseAssemblySheet> list = makeService.findAssemblySheetByQuery(assemblySheet);
        return jsonResult(list);
    }

    /**
     * @Author wsg
     * @Date 14:29 2021/5/6
     * @Description 装配件添加 页面
     **/
    @GetMapping("/makeAdd/{id}")
    @RequiresPermissions({"asseOrd:submit:edit"})
    public String viewPage(@PathVariable("id") Integer id, Model model) {
        AsseAssemblySheet asse = assemblySheetRepository.getOne(id);
        model.addAttribute("asse", asse);
        model.addAttribute("details", dispatchService.findAssemblyDetailBySheetId(id));
        model.addAttribute("buyList", makeService.findAsseAssemblyBuyList(id));
        return "assemble/make/makeAdd";
    }

    /**
     * @Author wsg
     * @Date 15:29 2021/5/6
     * @Description 制作件列表
     **/
    @ResponseBody
    @PostMapping("/makePieceList")
    public JsonResult makePieceList(AsseAssemblyMake assemblyMake) {
        startPage();
        List<AsseAssemblyMake> list = makeService.findAsseAssemblyMakeList(assemblyMake);
        return jsonResult(list);
    }

    /**
     * @Author wsg
     * @Date 15:29 2021/5/7
     * @Description 装配零件信息
     **/
    @ResponseBody
    @PostMapping("/makeChooseList")
    public JsonResult makeChooseList(HalfStore halfStore) {
        startPage();
        List<AsseAssemblyMake> list = makeService.findMakeChooseList(halfStore);
        return jsonResult(list);
    }

    /**
     * @Author wsg
     * @Date 15:29 2021/5/7
     * @Description 添加装配零件
     **/
    @ResponseBody
    @PostMapping("/addMake")
    @RequiresPermissions({"asseOrd:make:edit"})
    public JsonResult addMake(String arrIds, Integer sheetId) {
        return jsonResult(makeService.addMake(arrIds, sheetId));
    }

    /**
     * @Author wsg
     * @Date 13:29 2021/5/7
     * @Description 删除装配零件
     **/
    @ResponseBody
    @PostMapping("/delMake")
    @RequiresPermissions({"asseOrd:make:edit"})
    public JsonResult delMake(String arrIds, Integer sheetId) {
        return jsonResult(makeService.delMake(arrIds, sheetId));
    }

    /**
     * @Author wsg
     * @Date 15:43 2021/5/7
     * @Description 修改零件领用数量
     **/
    @ResponseBody
    @PostMapping("/editMakeUseNum")
    @RequiresPermissions({"asseOrd:make:edit"})
    public JsonResult editMakeUseNum(AsseAssemblyMake assemblyMake) {
        return jsonResult(makeService.editMakeUseNum(assemblyMake));
    }

    /**
     * @Author wsg
     * @Date 10:15 2021/5/8
     * @Description 保存装配外购件信息
     **/
    @ResponseBody
    @PostMapping("/saveBuy")
    @RequiresPermissions({"asseOrd:buy:edit"})
    public JsonResult saveBuy(Integer sheetId, String[] matNameDesc, String[] trackCode, Integer[] useNum
            , String[] unit, String[] backStatus, String[] position, String[] comment) {
        return jsonResult(makeService.saveBuy(sheetId, matNameDesc, trackCode, useNum, unit, backStatus, position, comment));
    }

    /**
     * @Author wsg
     * @Date 12:55 2021/5/7
     * @Description 领料计划查看 页面
     **/
    @GetMapping("/makeView/{id}")
    public String makeView(@PathVariable("id") Integer id, Model model) {
        AsseAssemblySheet asse = assemblySheetRepository.getOne(id);
        model.addAttribute("asse", asse);
        model.addAttribute("details", dispatchService.findAssemblyDetailBySheetId(id));
        model.addAttribute("buyList", makeService.findAsseAssemblyBuyList(id));
        String path = "";
        if (asse.getSpecial().equals("自动化")) {
            path = "assemble/make/makeAmsView";
        } else {
            path = "assemble/make/makeView";
        }
        return path;
    }

    /**
     * @Author wsg
     * @Date 13:03 2021/5/7
     * @Description 派工单领料计划提交 页面
     **/
    @GetMapping("/makeSubPage")
    @RequiresPermissions({"xxx"})
    public String makeSubPage() {
        return "assemble/make/makeSub";
    }

    /**
     * @Author wsg
     * @Date 13:46 2021/5/7
     * @Description 派工领料计划列表
     **/
    @ResponseBody
    @PostMapping("/makeListAll")
    public JsonResult makeListAll(MakeListModel makeListModel) {
        startPage();
        List<MakeListModel> list = makeService.findAsseAssemblyMakeListByQuery(makeListModel);
        return jsonResult(list);
    }

    /**
     * @Author wsg
     * @Date 15:29 2021/5/8
     * @Description 提交装配零件
     **/
    @ResponseBody
    @PostMapping("/subMake")
    @RequiresPermissions({"xxx"})
    public JsonResult subMake(String arrIds) {
        String msg = makeService.subMake(arrIds);
        JsonResult jsonResult = new JsonResult();
        if ("提交成功".equals(msg)) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功！");
        } else if ("被提交".equals(msg)) {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("被提交的数据不允许删除，请刷新页面后重新选择要删除的数据！");
        } else if ("库存不足".equals(msg)) {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("被提交的数据库存不足，红色即为库存不足的数据，请核对后再次提交！");
        }
        return jsonResult;
    }

    /**
     * @param id 撤销件主键
     * @Author wsg
     * @Date 15:29 2021/5/10
     * @Description 撤销装配零件
     **/
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"asseOrd:make:edit"})
    public JsonResult cancel(Integer id) {
        return jsonResult(makeService.cancelMake(id));
    }

    /**
     * @Author wsg
     * @Date 14:48 2021/5/11
     * @Description 派工单领料计划查询 页面
     **/
    @GetMapping("/makeSelPage")
    @RequiresPermissions({"asseOrd:makeSel:view"})
    public String makeSelPage() {
        return "assemble/make/makeSel";
    }

    /**
     * @Author wsg
     * @Date 09:29 2021/5/12
     * @Description 提交该单据未提交的装配零件
     **/
    @ResponseBody
    @PostMapping("/subMakeSheet")
    @RequiresPermissions({"asseOrd:make:edit"})
    public JsonResult subMakeSheet(Integer sheetId) {
        String msg = makeService.subMakeSheet(sheetId);
        JsonResult jsonResult = new JsonResult();
        if ("提交成功".equals(msg)) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功！");
        } else if ("库存不足".equals(msg)) {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("被提交的数据库存不足，红色即为库存不足的数据，请核对后再次提交！");
        }
        return jsonResult;
    }

    /**
     * @Author wsg
     * @Date 16:29 2021/5/20
     * @Description 保存装配零件信息列表 hot控件
     **/
    @ResponseBody
    @PostMapping("/saveMakeList")
    @RequiresPermissions({"asseOrd:make:edit"})
    public JsonResult saveMakeHotList(@RequestBody MakeListHotModel makeListHotModel) {
        JsonResult jsonResult = new JsonResult();
        String msg = makeService.saveMakeHotList(makeListHotModel);
        if (!msg.contains("保存失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

}
